<template>
  <div>
    <AplidForm ref="formRef" v-bind="ExtendFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { ExtendFormConfig } from '../../config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { setTempParams,setLoading } = useCommonStore()
const { getValue, getContent } = useOperate(2)
const nowRowData: any = inject('nowRowData')

const emit = defineEmits(['submit'])
const formModel = ref({
  ServiceOrgItems: [],
})
const formRef = ref(null)
let baseData = markRaw({})
let baseItems = markRaw({})
const onSubmit = (form, done) => {
  const { tempNowObj } = storeToRefs(useCommonStore())
  const params = {
    ...form,
  }

  // 处理变更字段
  let changeColumnsBase = ExtendFormConfig.column.filter((item) => item.changeField)

  let changeData = []
  changeColumnsBase.map((item) => {
    const tempBefore = baseData[item.propName || item.prop]
    const tempAfter = params[item.propName || item.prop]
    if (tempBefore !== tempAfter) {
      changeData.push({
        label: item.label,
        before: getValue(tempBefore, item),
        after: getValue(tempAfter, item),
      })
    }
  })

  const emptyObj = {
    creditCode: tempNowObj.value.creditCode,
    organizationName: tempNowObj.value.organizationName,
    code: undefined,
    areas_name: undefined,
    areas: undefined,
    ServiceOrgItems: undefined,
  }

  const changeObj = {
    data: changeData,
    dataChangeContentList: [
      ...getContent(baseItems, params.ServiceOrgItems, 3016, '养老机构-服务项目', emptyObj, 'id'),
      {
        changeBefore: {
          ...baseData,
          ...emptyObj,
        },
        changeAfter: {
          ...params,
          ...emptyObj,
        },
        changeData: {
          ...params,
          ...emptyObj,
        },
        changeType: '养老机构-扩展信息',
        code: 3015,
      },
    ],
  }
  setTempParams({
    creditCode: tempNowObj.value.creditCode,
    organizationName: tempNowObj.value.organizationName,
    name: tempNowObj.value.organizationName,
    nowChange: changeObj,
  })
  emit('submit', changeObj)
  done()
}

const onSave = () => {
  formRef.value!.onSubmit()
}

const getInitData = () => {
  let query = {
    creditCode: nowRowData.creditCode,
  }
  setLoading()
  Promise.all([apiMod.view_dpServiceOrgExtend_page(query), apiMod.view_dpServiceOrgItems_page(query)])
    .then((res) => {
      return res.map((item) => item.data.records)
    })
    .then((res) => {
      const extendData = res[0][0] || {}
      const tempItemData = res[1]
      formModel.value = extendData
      formModel.value.ServiceOrgItems = tempItemData

      baseData = Object.freeze(JSON.parse(JSON.stringify(extendData)))
      baseItems = Object.freeze(JSON.parse(JSON.stringify(tempItemData)))
      setLoading()
    })
}

onMounted(() => {
  getInitData()
})

defineExpose({
  onSave,
})
</script>

<style scoped></style>
